package com.googlecode.wmlynar.j2meunitgenerator;

import java.util.ArrayList;

/**
 * Generates TestSuite class given list of test cases in form of TestClass object array.
 * 
 * @author Wojciech Mlynarczyk (woj)
 * 
 */
public class TestSuiteGenerator {
	
	public String generate(String packageName, String className, ArrayList<TestClass> testClasses) {
		
		return
			generateHeader(packageName,className) + 
			generateSuiteMethod(packageName,className,testClasses) + 
			generateClosingBracket();
	}
	
	private String generateHeader(String packageName, String className) {
		StringBuilder builder = new StringBuilder();
		if(packageName!=null && !"".equals(packageName.trim())) {
			builder.append(
					"package " + packageName + ";\n" +
					"\n");
		}
		builder.append(
				"import j2meunit.framework.Test;\n" +
				"import j2meunit.framework.TestCase;\n" +
//				"import j2meunit.framework.TestMethod;\n" +
				"import j2meunit.framework.TestSuite;\n" +
				"\n" +
				"/**\n" +
				" *  This class is generated by j2meunit-generator, DO NOT MODIFY\n" +
				" *\n" +
				" */\n" +
				"public class " + className + " extends TestCase {\n" +
				"\n");
		return builder.toString();
	}

	private String generateSuiteMethod(String packageName, String className, ArrayList<TestClass> testClasses) {
		StringBuilder builder = new StringBuilder();
		builder.append(
			"	public Test suite() {\n" +
			"		TestSuite allSuites = new TestSuite(\"" + packageName + "." + className + "\");\n" +
			"		TestSuite suite;\n" +
			"\n");
		
		int suiteNum = 0;
		
		for(TestClass c : testClasses) {
			builder.append(
					"		// " + c.getName() + "\n" +
					"		suite = new TestSuite(\"" + c.getName() + "\");\n" +
					"		" + c.getName() + " test" + suiteNum + ";\n");
			
			for(String method : c.getTestMethods()) {
//				builder.append(
//						"		test" + suiteNum + " = new " + c.getName() + "();\n" +
//						"		test" + suiteNum + ".setName(\"" + method + "\");\n" +
//						"		test" + suiteNum + ".setTestMethod(new TestMethod() {\n" +
//						"			public void run(TestCase tc) throws Throwable {\n" +
//						"				((" + c.getName() + ") tc)." + method + "();\n" +
//						"			}\n" +
//						"		});\n" +
//						"		suite.addTest(test" + suiteNum + ");\n");
//				builder.append(
//						"		test" + suiteNum + " = new " + c.getName() + "() {\n" +
//						"			public String toString() {\n" +
//						"				return \"" + method + "(" + c.getName() + ")\";\n" +
//						"			}\n" +
//						"		};\n" +
//						"		test" + suiteNum + ".setName(\"" + method + "\");\n" +
//						"		test" + suiteNum + ".setTestMethod(new TestMethod() {\n" +
//						"			public void run(TestCase tc) throws Throwable {\n" +
//						"				((" + c.getName() + ") tc)." + method + "();\n" +
//						"			}\n" +
//						"		});\n" +
//						"		suite.addTest(test" + suiteNum + ");\n");
				builder.append(
						"		test" + suiteNum + " = new " + c.getName() + "() {\n" +
						"			public void runTest() throws Throwable {\n" +
						"				" + method + "();\n" +
						"			}\n" +
						"			public String toString() {\n" +
						"				return \"" + method + "(" + c.getName() + ")\";\n" +
						"			}\n" +
						"		};\n" +
						"		test" + suiteNum + ".setName(\"" + method + "\");\n" +
						"		suite.addTest(test" + suiteNum + ");\n");
			}
			
			builder.append(
					"		allSuites.addTest(suite);\n");
			suiteNum++;
		}
				
		builder.append(
			"\n" +
			"		return allSuites;\n" +
			"	}\n");
		return builder.toString();
	}

	private String generateClosingBracket() {
		return "}\n";
	}

}
